package edu.qichen.onlineBlog.log.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.qichen.onlineBlog.framework.mybatis.core.query.LambdaQueryWrapperX;
import edu.qichen.onlineBlog.log.dto.SyslogInfoDTO;
import edu.qichen.onlineBlog.log.mapper.SyslogMapper;
import edu.qichen.onlineBlog.log.pojo.Syslog;
import edu.qichen.onlineBlog.log.service.SyslogService;
import edu.qichen.onlineBlog.log.vo.PageSyslogReq;
import edu.qichen.onlineBlog.log.vo.PageSyslogRes;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能:
 * 作者:程序员欧阳子韩
 * 日期:2024/5/1 13:45
 */
@Service
public class SyslogServiceImpl extends ServiceImpl<SyslogMapper, Syslog> implements SyslogService {
    @Resource
    private SyslogMapper syslogMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insetAll(List<Syslog> operaLogs) {
        for (Syslog operaLog : operaLogs) {
            baseMapper.insert(operaLog);
        }
    }

    @Override
    public PageSyslogRes selectPage(PageSyslogReq pageSyslogReq) {
        Page<Syslog> page = new Page<>(pageSyslogReq.getPageNum(), pageSyslogReq.getPageSize());

        LambdaQueryWrapperX<Syslog> wrapperX = new LambdaQueryWrapperX<>();

        wrapperX.orderByDesc(Syslog::getCreateTime);

        Page<SyslogInfoDTO> logPage = syslogMapper.selectPageInfo(page, wrapperX);


        return PageSyslogRes
                .builder()
                .syslogs(logPage.getRecords())
                .pageNum(logPage.getCurrent())
                .pages(logPage.getPages())
                .pageSize(logPage.getSize())
                .total(logPage.getTotal())
                .build();
    }
}
